package com.tanglon.service.impl;

import com.alibaba.excel.EasyExcel;
import com.tanglon.domain.dto.ExcelUserDTO;
import com.tanglon.listener.read.MutliReadListener;
import com.tanglon.listener.read.SimpleReadListener;
import com.tanglon.service.ExcelReadService;
import com.tanglon.service.UserService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

@Service
public class ExcelReadServiceImpl implements ExcelReadService {


    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private UserService userService;

    @Override
    public void readSimpleExcel(MultipartFile file) {
        try{
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, ExcelUserDTO.class,new SimpleReadListener()).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void readMultipartExcel(MultipartFile file) {
        try{
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, ExcelUserDTO.class,new MutliReadListener(passwordEncoder, userService)).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
